<?php
  class MembersController extends Zend_Controller_Action{
  	protected $_model;
  	
    public function init(){
      $authNamespace = new Zend_Session_Namespace('Zend_Auth');
      
      /*if(!$authNamespace->session_initialized){
        $this->_redirect('/account/login');
      }*/ 
    }
    
    // Método retorna a model de usuario.
    protected function _getModel(){
      if($this->_model === null){
        $this->_model = new Application_Model_Members();
      }
      
      return $this->_model;
    }
    
    public function indexAction(){
    	$objDaoMembers = new Application_Model_Members();
      $arrMembers = $objDaoMembers->getMembers();
      
      $this->view->arrMembers = $arrMembers;
    }
    
    public function sendMail(){
      $conf_smtp = array(
              'auth'      => 'login',
              'username'  => 'evandrojlle@gmail.com',
              'password'  => 'ely@2002',
              'ssl'       => 'tls', 
              'port'      => '587'
            );
            
            $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $conf_smtp);
            
            $smtp = new Zend_Mail();
            //$smtp->setCharset("iso-8859-1");
            //$smtp->setDate(date("d/m/Y H:i:s"));
            $smtp->setFrom('root@localhost.com', 'Root');
            $smtp->addTo('evandrojlle@gmail.com', 'Evandro de Oliveira');
            $smtp->setSubject("TESTE DE ENVIO");
            $smtp->setBodyHtml("<p>VAMO QUE VAMO</p>");
    }
    
    public function listMembersAction(){
    	$model = $this->_getModel();
    	
      $select = "";
      
      $filter = new Zend_Filter_StripTags();
      
      if($this->_request->isPost()){
        $ds_login = $filter->filter($this->_request->getPost('ds_login'));
        
        $st_member = $filter->filter($this->_request->getPost('hdn_st_member'));
      }
      else{
        $ds_login = "";
      }
      
      try{
        $select = $model->listData($ds_login, $st_member);   
      }
      catch(TSS_FormException $e){
        $this->view->box(
          array(
            'message' => $e->getMessage(),
            'icon'    => $e->getCode(),
            'link'    => $e->getReturn()
          )
        );
      }
      
      if(!$select){
        $select = 0;
      }
      // Criando a paginação.
      Zend_Paginator::setDefaultScrollingStyle('Sliding');
      Zend_View_Helper_PaginationControl::setDefaultViewPartial('members/paginator_control.phtml');

      $paginator = Zend_Paginator::factory($select);
      
      //Pagina atual. Se não houver nenhuma pagina, mostra a primeira.
      $currentPage = $this->_getParam('page', 1);
      
      $paginator->setCurrentPageNumber($currentPage)->setItemCountPerPage(Zend_Registry::getInstance()->config->default->result->per->page);
      
      $this->view->paginator = $paginator;  
      //$this->view->data = $this->_getModelU()->listDataBySearch($ds_criterio);
    }
    
    public function formInsertMemberAction(){
      $mdlOrganization = new Application_Model_Organizations();
      
      $this->view->organizations = $mdlOrganization->getAllOrgs();
      
      $mdlProfile = new Application_Model_Profiles();
      
      $this->view->profiles = $mdlProfile->getAllProfiles();
    }
    
    public function insertMemberAction(){
    	$filter = new Zend_Filter_StripTags();
    	
    	$mdlMembers = $this->_getModel();
    	
      if($this->_request->isPost()){
        $post = $this->_request->getPost();
        
        if(count($post) > 0){
          try{
            $insert = $mdlMembers->insertData();
            
            if($insert){
              $this->view->box(
                array(
                  'message' => 'Registro inserido com sucesso',
                  'icon'    => 'info',
                  'link'    => 'form-insert-member'
                )
              );
            }
            else{
              $this->view->box(
                array(
                  'message' => 'Ocorreu algum problema. Volte e tente novamente',
                  'icon'    => 'error',
                  'link'    => 'form-insert-member'
                )
              );
            }
          }
          catch(Core_FormException $e){
          	$this->view->box(
              array(
                'message' => $e->getMessage(),
                'icon'    => $e->getCode(),
                'link'    => $e->getReturn()
              )
            );
          }
        }
      }
    }
    
    public function formUpdateMemberAction(){
    	if($this->_request->isPost()){
    		$cod = $this->_request->getPost('cod');
    		
    		$mdlMembers = $this->_getModel();
    		
    		if($cod){
    			$this->view->data = $mdlMembers->getDataById($cod);
    			
    			$mdlOrganization = new Application_Model_Organizations();
      
          $this->view->organizations = $mdlOrganization->getAllOrgs();
          
          $mdlProfile = new Application_Model_Profiles();
      
          $this->view->profiles = $mdlProfile->getAllProfiles();
    		}
    		else{
    			$this->view->box(
    			  array(
    			    'message' => "",
    			    'icon'    => "info",
    			    'link'    => $this->view->baseUrl . "/administration/show-data"
            )
          );
    		}
    	}
    }
    
    public function updateMemberAction(){
      $filter = new Zend_Filter_StripTags();
      
      $mdlMembers = $this->_getModel();
      
      if($this->_request->isPost()){
        $post = $this->_request->getPost();
        
        echo"<pre>";
        print_r($post);
        echo"</pre>";
      }
    }
  }  